[#ftl]
[@define "agile.MetadataEditorPane" "ContentPane" ]		
	parseOnLoad:false	
	,widgetsInTemplate:true
	,templateString:"<div><table class='editorContainer' style='width:100%;cell-spacing:0px;cell-padding:0px;border:0px' data-dojo-attach-point='editorContainer'></table></div>"	
	,category:null
	,currentWidget:null
	,editorWidgets : null
	,constructor:function(){
		this.declaredClass = this.constructor.prototype.declaredClass;
		this.editorWidgets = {};		
		this.title = agile.i18n(this.title,null,true);		
		//stop propagation for key event from metadata editor pane
		aspect.after(this,"startup",lang.hitch(this,function(){
			agile.stopKeyEvent(this);
			this.init()
		}));
	}
	,postCreate:function(){
		this.domNode.innerHTML = this.templateString;
//    	//console.log(this.declaredClass,"innerHTML:",this.domNode.innerHTML);
    	parser.parse(this.domNode);    	
    	this.editorContainer = query(".editorContainer",this.domNode)[0];
//    	//console.log(this.declaredClass,"editorContainer:",this.editorContainer)
	}
	,init:function(){
		this.subscribe("agile.widget.selected",function(widget){
//			console.error("agile.widget.selected",this.title,this.currentWidget?this.currentWidget.shortName:"@","->",widget?widget.shortName:"@")
			if(this.currentWidget != widget){
//				if(this.currentWidget != null){
//					this.currentWidget.onDeselected();
//				}
				this.currentWidget = widget;
				this.onWidgetSelected(widget);				
			}
		});	
	
		this.subscribe("agile.widget.removed",function(widget){
			if(this.currentWidget == widget){				
				this.currentWidget = null;		
			}
		});
	}
	[@method "onWidgetSelected"/]
	,onWidgetSelected:function(widget){		
		if(has("ie") < 11){
			//TODO fix me: release editor widgets for reuse
			for(var name in this.editorWidgets){
				//this.editorWidgets[name].release();
				construct.destroy(this.editorWidgets[name].domNode);
				delete this.editorWidgets[name];
			}	
		}
		else{
			for(var name in this.editorWidgets){				
				this.editorWidgets[name].release();								
			}		
		}
		//reset the editor cache
		this.editorWidgets = {};
		//remove the editor widget from screen
		while (this.editorContainer.rows.length > 0){
			this.editorContainer.deleteRow(0);
	    }
			
		if(widget != null){
			this.renderEditors(widget);
			this.setEditorValue(widget);
		}
	}
	[@method "renderEditors"/]
	,renderEditors:function(/*object*/widget){	
        var attributes = this.getAttributes(widget);
		for(var name in attributes){
			var metadata = attributes[name];
//			console.log("#########renderEditor===",name,metadata,widget.declaredClass);
			if(metadata.hidden(widget)) continue;
			var editor = agile.EditorFactory.lookupEditorWidget(metadata,this);
			//this.editorWidgets[name] = editor;
			lang.mixin(this.editorWidgets,editor.getEditors());			
//			console.log("#########renderEditor===editorWidgets=",this.editorWidgets);
			
			var tr = this.editorContainer.insertRow(this.editorContainer.rows.length);						
	        var td = tr.insertCell(tr.cells.length);		        
	        td.appendChild(editor.domNode);	
		}		
	}
	[@method "setEditorValue"/]
	,setEditorValue:function(widget){		
		var attributes = this.getAttributes(widget);
		var editors = this.editorWidgets;		
		for (var name in editors){
			if(attributes[name]){				
				editors[name].reset1("value",attributes[name].format(widget.metadata(name)));
			}
		}		
	}
	,getAttributes:function(widget){
		return widget[this.category]();
	}
	[@method "getCurrentWidget"/]
	,getCurrentWidget:function(){
		return this.currentWidget;
	}
[/@]